package org.ihelpuoo.weboo.upload;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.ihelpuoo.lang.LoaderException;

class FieldValue
{
	static void load(InputStream ins, OutputStream ops, int[] segs, int[] endl)
			throws LoaderException
	{
		int index = 0;
		int[] buffer = new int[endl.length];
		int b;
		try
		{
			while (-1 != (b = ins.read()))
			{
				if ((index < segs.length && segs[index] == b) || endl[index] == b)
				{
					buffer[index++] = b;
				}
				else
				{
					if (index > 0)
					{
						for (int i = 0; i < index; i++)
							ops.write(buffer[i]);
						index = 0;
					}
					ops.write(b);
				}
				if (index == segs.length)
				{
					if (segs[index - 1] == buffer[index - 1])
						break;
				}
				else if (index == endl.length)
					break;
			}
			ops.close();
			ops = null;
		}
		catch (IOException e)
		{
			throw new LoaderException(e);
		}
		finally
		{
			if (null != ops)
				try
				{
					ops.close();
				}
				catch (IOException e)
				{
					throw new LoaderException(e);
				}
		}
	}
}
